package dhbw.GUI.Panels;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

import dhbw.Data.Language;
import dhbw.Data.Pruefung;
import dhbw.Data.Settings;
import dhbw.Data.Vorlesung;
import dhbw.GUI.TableModels.EntryStudentLeistungsuebersicht;
import dhbw.GUI.TableModels.TableModelStudent;
import dhbw.GUI.TableModels.TableRendererNoten;
import dhbw.Roles.Student;
import Main.MyController;

public class PanelStudentSemesteruebersicht extends JPanel implements
		ItemListener {

	private MyController myc;
	private JTable table;
	private JComboBox cb_semester;
	private JLabel durchschnitt;

	public PanelStudentSemesteruebersicht(MyController myc) {
		this.myc = myc;
		build();
	}

	/**
	 * Build Panel
	 */
	private void build() {
		this.setBackground(Color.WHITE);
		JPanel auswahlPanel = new JPanel(new BorderLayout());
		auswahlPanel.setBackground(Settings.BG_COLOR);
		auswahlPanel.add(new JLabel("Auswahl:"));
		cb_semester = new JComboBox();
		for (int i = 1; i < 7; i++) {
			cb_semester.addItem("Semester " + i);
		}
		cb_semester.setSize(200, 25);
		cb_semester.addItemListener(this);
		durchschnitt = new JLabel("---");
		auswahlPanel.add("West", durchschnitt);
		JPanel filler = new JPanel();
		filler.setPreferredSize(new Dimension(600, 20));
		filler.setBackground(Settings.BG_COLOR);
		auswahlPanel.add("East", filler);
		// auswahlPanel.add("West", new
		// JLabel(Language.CHOICE[this.myc.getLanguage()]));
		auswahlPanel.add("Center", cb_semester);

		auswahlPanel.add("North", new JLabel(" "));
		auswahlPanel.add("South", new JLabel(" "));
		table = new JTable();
		this.setLayout(new BorderLayout());
		TableModelStudent model = new TableModelStudent(this.myc,
				getSemesterList(1));
		table.setModel(model);
		DefaultTableCellRenderer ren = new TableRendererNoten();
		table.setDefaultRenderer(Object.class, ren);
		table.getColumnModel().getColumn(0).setPreferredWidth(500);
		table.setFocusable(false);
		table.setShowVerticalLines(true);
		table.setShowHorizontalLines(true);
		table.setRowHeight(30);
		table.setAutoCreateRowSorter(true);
		this.add("North", auswahlPanel);
		this.add("Center", new JScrollPane(table));
		durchschnitt.setText(Language.AVERAGE[this.myc.getLanguage()]
				+ getSchnitt() + "                    ");
	}

	/**
	 * Get the overview from a specific semester as ArrayList
	 * 
	 * @param semester
	 *            - int
	 * @return Returns an ArrayList with {@link EntryStudentLeistungsuebersicht}
	 */
	private ArrayList<EntryStudentLeistungsuebersicht> getSemesterList(
			int semester) {
		ArrayList<EntryStudentLeistungsuebersicht> mainlist = new ArrayList<EntryStudentLeistungsuebersicht>();
		HashMap<Vorlesung, ArrayList<Pruefung>> exam = ((Student) this.myc
				.getActiveUser()).getExamResults();
		for (Vorlesung vl : exam.keySet()) {
			if (exam.get(vl).size() > 0) {
				Pruefung p1 = ((ArrayList<Pruefung>) exam.get(vl)).get(exam
						.get(vl).size() - 1);
				if (vl.getSemester() == semester) {
					mainlist.add(new EntryStudentLeistungsuebersicht(vl
							.getBezeichnung(), p1.getErgebniss(), myc
							.getNotenspiegel().getNote(p1.getErgebniss())));
				}
			}

		}
		return mainlist;
	}

	/**
	 * Get Average from actual model
	 * 
	 * @return Returns the Average of the actual Model
	 */
	private int getSchnitt() {
		int rtn = 0;
		int count = 0;
		try {
			for (int i = 0; i < table.getModel().getRowCount(); i++) {
				count++;
				rtn = rtn
						+ Integer.parseInt(table.getModel().getValueAt(i, 2)
								.toString());
			}
			rtn = Math.round(rtn / count);
		} catch (Exception ex) {
		}
		return rtn;
	}

	@Override
	public void itemStateChanged(ItemEvent e) {
		if (e.getStateChange() == ItemEvent.DESELECTED) {
			System.out.println(Integer.parseInt(cb_semester.getSelectedItem()
					.toString().split(" ")[1]));
			table.setModel(new TableModelStudent(myc, getSemesterList(Integer
					.parseInt(cb_semester.getSelectedItem().toString()
							.split(" ")[1]))));
			durchschnitt.setText(Language.AVERAGE[this.myc.getLanguage()]
					+ getSchnitt() + "                    ");
		}
	}
}